<!DOCTYPE>
<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
 <meta charset="utf-8">
  <title> WebSocket Html5 </title>
  <meta name="generator" content="editplus" />
  <meta name="author" content="" />
  <meta name="keywords" content="" />
  <meta name="description" content="" />
 </head>

<script type="text/javascript">
<!--
 var  wsServer = 'ws://localhost:8888/Demo'; 
 
 //创建Socket实例
 var  websocket = new WebSocket(wsServer); 
 websocket.onopen = function (evt) { onOpen(evt) }; 
 websocket.onclose = function (evt) { onClose(evt) }; 
 websocket.onmessage = function (evt) { onMessage(evt) }; 
 websocket.onerror = function (evt) { onError(evt) }; 

 //打开 Socket
 function onOpen(evt) {
	//发送一个初始化消息
	websocket.send('I am the client and I\'m listening!'); 
	console.log("Connected to WebSocket server."); 
	//关闭 socket
	websocket.colse();
 }  
 //监听关闭
 function onClose(evt) { 
	console.log("Disconnected"); 
 } 

 //监听消息
 function onMessage(evt) { 
	console.log('Retrieved data from server: ' + evt.data); 
 } 
 //监听错误
 function onError(evt) { 
	console.log('Error occured: ' + evt.data); 
 } 



//-->
</script>


 <body>




 </body>
</html>
